package com.m3839.sdk.common.env2;

import android.text.TextUtils;
import android.util.Pair;

import com.m3839.sdk.common.core.Constant;
import com.m3839.sdk.common.core.SdkConfiguration;
import com.m3839.sdk.common.env.EnvConfig;
import com.m3839.sdk.common.env2.platform.AntiPlatformApi;
import com.m3839.sdk.common.env2.platform.KuaiBaoPlatformApi;
import com.m3839.sdk.common.env2.platform.YuanChuangPlatformApi;
import com.m3839.sdk.common.util.VerifyUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * @author ChenBaoyang
 * @description: ApiManager
 * @date 2021/11/6 10:32
 */
public class ApiManager {

    /**
     * 包名
     */
    private static final String KEY_PACKAGE_NAME = "packageName";

    /**
     * sdk版本号
     */
    private static final String KEY_SDK_VERSION = "sdkVersion";

    /**
     * sdk接口header头上报用户信息
     */
    private static final String KEY_HOST = "KBU";

    private static final String WEB_LOGIN_PATH = "sdk/login.php?orientaion=%s&gid=%s&key=%s&version=%s";
    private static final String WEB_REAL_NAME_PATH = "sdk/idcard.php?orientaion=%s&gameId=%s&uid=%s&type=%s&token=%s";

    /**
     * 当前环境变量
     */
    private int currentEnv;

    private IPlatformApi yuanChuangApi;

    private IPlatformApi kuaiBaoApi;

    private IPlatformApi antiApi;

    private IPlatformApi zhuZhanApi;

    private ApiManager() {
        // 默认
        setDevMode();
    }

    private static class SingletonHolder {
        private static final ApiManager INSTANCE = new ApiManager();
    }

    /**
     * 返回GlobalManager对象
     */
    public static ApiManager getInstance() {
        return SingletonHolder.INSTANCE;
    }

    public void setDevMode() {
        currentEnv = Env.ENV_TEST;
    }

    public void setOnlineTestMode() {
        currentEnv = Env.ENV_ONLINE_TEST;
    }

    public void setOfficialMode() {
        currentEnv = Env.ENV_OFFICIAL;
    }

    /**
     * 获取原创平台的API配置信息
     *
     * @return
     */
    public EnvConfig getYuanChuangApiEnvConfig() {
        if (yuanChuangApi == null) {
            yuanChuangApi = new YuanChuangPlatformApi();
        }
        return yuanChuangApi.getApi(currentEnv);
    }

    /**
     * 获取快爆平台的API配置信息
     *
     * @return
     */
    public EnvConfig getKuaiBaoApiEnvConfig() {
        if (kuaiBaoApi == null) {
            kuaiBaoApi = new KuaiBaoPlatformApi();
        }
        return kuaiBaoApi.getApi(currentEnv);
    }

    /**
     * 获取防沉迷的API配置信息
     *
     * @return
     */
    public EnvConfig getAntiApiEnvConfig() {
        if (antiApi == null) {
            antiApi = new AntiPlatformApi();
        }
        return antiApi.getApi(currentEnv);
    }

    /**
     * 获取主战的API配置信息
     *
     * @return
     */
    public EnvConfig getZhuZhanApiEnvConfig() {
        if (zhuZhanApi == null) {
            zhuZhanApi = new AntiPlatformApi();
        }
        return zhuZhanApi.getApi(currentEnv);
    }

    /**
     * 根据urlPath获取原创平台的API接口地址
     *
     * @param urlPath
     * @return
     */
    public String buildYuanChuangUrl(String urlPath) {
        return getYuanChuangApiEnvConfig().url + urlPath;
    }

    /**
     * 根据urlPath获取快爆平台的API接口地址
     *
     * @param urlPath
     * @return
     */
    public String buildKuaiBaoUrl(String urlPath) {
        return getKuaiBaoApiEnvConfig().url + urlPath;
    }

    /**
     * 根据urlPath获取防沉迷的API接口地址
     *
     * @param urlPath
     * @return
     */
    public String buildAntiUrl(String urlPath) {
        return getAntiApiEnvConfig().url + urlPath;
    }

    /**
     * 获取web登录页面的地址
     *
     * @return
     */
    public String buildWebLoginUrl() {
        String formatUtl = getZhuZhanApiEnvConfig().url + WEB_LOGIN_PATH;
        String url = String.format(formatUtl,
                SdkConfiguration.getOrientation(),
                SdkConfiguration.getGameId(),
                VerifyUtils.VERIFY_KEY,
                SdkConfiguration.getSDKVersion());
        return url;
    }

    /**
     * 获取web实名认证页面的地址
     *
     * @return
     */
    public String buildWebRealNameUrl(String uid, String loginType, String token) {
        String formatUrl = getZhuZhanApiEnvConfig().url + WEB_REAL_NAME_PATH;
        String url = String.format(formatUrl,
                SdkConfiguration.getOrientation(),
                SdkConfiguration.getGameId(),
                uid,
                loginType,
                token);
        return url;
    }

    /**
     * 根据urlPath和host 获取对应的API接口地址
     *
     * @param host
     * @param urlPath
     * @return
     */
    public String buildUrl(String host, String urlPath) {
        return host + urlPath;
    }

    /**
     * @param userId  快爆用户ID
     * @param urlPath API的地址host外的部分
     * @return
     */
    public Map<String, Object> getCommonParam(String userId, String urlPath) {
        long timeStamp = System.currentTimeMillis();
        String gameId = SdkConfiguration.getGameId();
        String sign = VerifyUtils.getVerifySign(gameId, userId, urlPath, String.valueOf(timeStamp));
        final Map<String, Object> param = new HashMap<>();
        param.put(Constant.HYKB_KEY_GAME_ID, gameId);
        param.put(Constant.HYKB_KEY_TIME, timeStamp);
        param.put(Constant.HYKB_KEY_SIGN, sign);
        return param;
    }

    /**
     * 获取授权的header信息
     *
     * @return
     */
    public Map<String, String> getCommonHeaders(EnvConfig config) {
        Map<String, String> headerMap = new HashMap<>();
        headerMap.put(KEY_PACKAGE_NAME, SdkConfiguration.getPackageName());
        headerMap.put(KEY_SDK_VERSION, SdkConfiguration.getSDKVersion());
        if (config.envHeader != null) {
            headerMap.put(config.envHeader.first, config.envHeader.second.toString());
        }
        return headerMap;
    }

    public Map<String, String> getAuthHeaders(EnvConfig config, String userState) {
        Map<String, String> headerMap = getCommonHeaders(config);
        // 通过header头添加加密的用户数据
        if (!TextUtils.isEmpty(userState)) {
            headerMap.put(KEY_HOST, userState);
        }
        return headerMap;
    }

    /**
     * 完整的接口地址拼接
     *
     * @param config
     * @return
     */
    public void checkParam(EnvConfig config, Map<String, Object> paramMap) {
        Pair<String, Object> param = config.envParam;
        if(param != null) {
            paramMap.put(param.first, param.second);
        }
    }

}
